//
//  Paths.swift
//  SitPosition
//
//  Created by apple on 2018/12/23.
//  Copyright © 2018 apple. All rights reserved.
//

import Foundation


/**
 * 目录集中管理类
 */
struct Paths {
    
    static let documentPath: String = NSSearchPathForDirectoriesInDomains(
        FileManager.SearchPathDirectory.documentDirectory,
        FileManager.SearchPathDomainMask.userDomainMask,
        true).first!
    
    static let libraryPath: String = NSSearchPathForDirectoriesInDomains(
        FileManager.SearchPathDirectory.libraryDirectory,
        FileManager.SearchPathDomainMask.userDomainMask,
        true).first!
    
    static let cachePath: String = NSSearchPathForDirectoriesInDomains(
        FileManager.SearchPathDirectory.cachesDirectory,
        FileManager.SearchPathDomainMask.userDomainMask,
        true).first!
    
    static let tempPath: String = NSTemporaryDirectory()
    
    // 持久化缓存目录
    static func persistentPath(_ filename: String? = nil) throws -> String {
        let path = (libraryPath as NSString).appendingPathComponent("persistent")
        if !FileManager.default.fileExists(atPath: path) {
            try FileManager.default.createDirectory(atPath: path, withIntermediateDirectories: false, attributes: nil)
        }
        if let filename_ = filename {
            return (path as NSString).appendingPathComponent(filename_)
        } else {
            return path
        }
    }
    
    // 临时缓存目录
    static func tempCachePath(_ filename: String? = nil) throws -> String {
        let path = (tempPath as NSString).appendingPathComponent("persistent")
        if !FileManager.default.fileExists(atPath: path) {
            try FileManager.default.createDirectory(atPath: path, withIntermediateDirectories: false, attributes: nil)
        }
        if let filename_ = filename {
            return (path as NSString).appendingPathComponent(filename_)
        } else {
            return path
        }
    }
    
    // SQLite数据库
    static func dbPath() throws -> String {
        return try Paths.persistentPath("db.sqlite3")
    }
    
    // 用户学习配置缓存目录
    static func userConfigsPath(_ userId: String? = nil) throws -> String {
        let path = try Paths.persistentPath("userConfigs")
        if !FileManager.default.fileExists(atPath: path) {
            try FileManager.default.createDirectory(atPath: path, withIntermediateDirectories: false, attributes: nil)
        }
        if let userId_ = userId {
            return (path as NSString).appendingPathComponent(userId_)
        } else {
            return path
        }
    }
    
    // 七牛上传缓存目录
    static func snapshotCachePath() throws -> String {
        let path = try Paths.tempCachePath("snapshots")
        if !FileManager.default.fileExists(atPath: path) {
            try FileManager.default.createDirectory(atPath: path, withIntermediateDirectories: false, attributes: nil)
        }
        return path
    }
    
    // 七牛上传缓存目录
    static func uploadCachePath() throws -> String {
        let path = try Paths.persistentPath("upcache")
        if !FileManager.default.fileExists(atPath: path) {
            try FileManager.default.createDirectory(atPath: path, withIntermediateDirectories: false, attributes: nil)
        }
        return path
    }
}
